package com.googlecode.transmutant;

import org.junit.Test;

import static org.junit.Assert.*;

class StepperTest {
	
	@Test
	public void testTypeOrder() throws Exception {
		def step1 = new TimeStep1()
		def step2 = new TimeStep2()
		new Stepper().step([step2,step1])
		assert step2.mosdef == "mosdef"
	}
	
	@Test
	public void testNoAnnotations() {
		SimpleStep step = new SimpleStep()
		step.step()
	}
	
	@Test
	public void testExecuteSimpleStep() {
		def step = new SimpleStep()
		new Stepper().step(step)
		assert step.pork == "dork"
	}
	
	@Test
	public void testSubclassStep() {
		def step = new SubclassStep()
		new Stepper().step(step)
		assert step.pork == "dorkmork"
	}
	
	@Test
	public void testStepOrder() {
		def step = new TwoStep()
		new Stepper().step(step)
		assert step.string == "step1step2"
	}
	
	@Test
	public void testStepOrderHigh() {
		def step = new ThreeStep()
		new Stepper().step(step)
		assert step.string == "step1step2step5"
	}
	
	@Test
	public void testStepOrderSomeAreSameOrder() {
		def step = new ThreeStepSame()
		new Stepper().step(step)
		assert step.string == "step1step2step5"
	}
	
	@Test
	public void testStepWithOutputField() throws Exception {
		def step1 = new OutStep()
		def step2 = new InStep()
		def steps = [step1, step2]
		new Stepper().step(steps)
		assert step2.pork == "pigmouth"
	}
	
	@Test
	public void testStepWithOutputFieldWithSetName() throws Exception {
		def step1 = new OutStep()
		def step2 = new InStep()
		def steps = [step1, step2]
		new Stepper().step(steps)
		assert step2.smorking == "smorking"
	}
	
	@Test
	public void testInputParameterOnMethod() throws Exception {
		def step1 = new OutStep()
		def step2 = new InStep()
		def steps = [step1, step2]
		new Stepper().step(steps)
		assert step2.spam == "spammified"
	}
	
	@Test
	public void testOutpuParameterOnMethod() throws Exception {
		def step1 = new MethodOutStep()
		def step2 = new InStep()
		def steps = [step1, step2]
		new Stepper().step(steps)
		assert step2.spam == "dingDong"
	}
	
	@Test
	public void testLogsNullInField() throws Exception {
		def bos = new ByteArrayOutputStream()
		System.setOut new PrintStream(bos)
		new Stepper().step new NullInsStep()
		assert bos.toString().contains("@In 'nilly' of 'class com.googlecode.transmutant.NullInsStep' is null")
	}
	
	@Test
	public void testLogsNullOutParameters() throws Exception {
		def bos = new ByteArrayOutputStream()
		System.setOut new PrintStream(bos)
		new Stepper().step new NullInsStep()
		assert bos.toString().contains("@Out 'nully' of 'class com.googlecode.transmutant.NullInsStep' is null")
	}
	
	@Test
	public void testLogsNullOutMethod() throws Exception {
		def bos = new ByteArrayOutputStream()
		System.setOut new PrintStream(bos)
		new Stepper().step new NullInsStep()
		assert bos.toString().contains("@Out 'nelly' on method 'fritatta' of 'class com.googlecode.transmutant.NullInsStep' is null")
	}
	
	@Test
	public void testLogsNullInParameters() throws Exception {
		def bos = new ByteArrayOutputStream()
		System.setOut new PrintStream(bos)
		new Stepper().step new NullInsStep()
		assert bos.toString().contains("@In 'nally' on method 'fritatta' of 'class com.googlecode.transmutant.NullInsStep' is null")
	}
	
	@Test
	public void testLogsNullNonInParameters() throws Exception {
		def bos = new ByteArrayOutputStream()
		System.setOut new PrintStream(bos)
		new Stepper().step new NullInsStep()
		assert bos.toString().contains("Found non @In parameters on method 'fritatta' of 'class com.googlecode.transmutant.NullInsStep'. These will be set to null")
	}
}
